<?php
/*
Copyright 2007 Benjamin V King

BH Widgets is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

BH Widgets is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with BH Widgets; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

require_once('BH_Widget_Base.php');

class BH_Category_Dropdown_Widget extends BH_Controllable_Widget_Base {
		
	static function get_categories() {
		global $wpdb;
		$sql = "select * from ". $wpdb->terms ." where term_id in (select term_id from ". $wpdb->term_taxonomy ." where taxonomy='category')";
		$sql .=" order by name";
		
		return $wpdb->get_results($sql, OBJECT);
	}

	function widget($args) {
		global $wpdb;
		extract($args);
		
		$all = $this->get_attribute('all');
		$selected_categories = $this->get_attribute('selected-categories');
		
		if (!$selected_categories && !$all) return;

		echo "$before_widget\n";
		echo $this->get_widget_start_tag($args);

		echo "<form action=\"\" method=\"\" name=\"catForm\" >\n";
		echo "<select name=\"catURL\" onchange=\"window.location=(document.forms.catForm.catURL[document.forms.catForm.catURL.selectedIndex].value);\" >\n";
		echo "<option>select category</option>\n";
		
		foreach(self::get_categories() as $category) {
			if (!$all && !in_array($category->term_id, $selected_categories))
				continue;
		
			$select =  $category->term_id == get_query_var('cat') ? " selected=\"true\"" : "";
			echo "<option value=\"". get_category_link($category->term_id) ."\"$select>";
			echo $category->name ."</option>\n";
		}

		echo "</select>\n";
		echo "</form>\n";
		
		echo $this->get_widget_end_tag($args);
		
		echo "$after_widget\n";
	}

	function control() {
		if ( $this->get_request_param_value('submit') ) {
			$this->update_attribute_from_param('selected-categories');
			$this->update_attribute_from_param('all');
		}
		
		$selected_categories = $this->get_attribute('selected-categories');
		$all = $this->get_attribute('all');

		echo "<label>All Categories:</label><input type=\"checkbox\" name=\"" . $this->get_request_param_name('all') . "\" ". ($all=='1' ? 'checked' : '') ." value=\"1\"/><br/>\n";
		echo "<select name=\"" . $this->get_request_param_name('selected-categories') . "[]\" size=\"5\" multiple >\n";

		foreach(self::get_categories() as $category) {
			$selected='';
			if (in_array($category->term_id, $selected_categories)) {
				$selected='selected="true"';
			}

			echo "<option value=\"". $category->term_id ."\" ". $selected .">". $category->name ."</option>\n";
		}

		echo "</select>\n";
		echo "<input type=\"hidden\" id=\"" . $this->get_request_param_name('submit') . "\" name=\"" . $this->get_request_param_name('submit') . "\" value=\"1\" />\n";
	}
}

bh_widgets_register_class('BH_Category_Dropdown_Widget');
?>
